package homework.jdbc.dao;

import com.sun.org.apache.xpath.internal.operations.Bool;
import homework.jdbc.domain.User;

import java.sql.*;
import java.util.*;

public class UserDao {
    public List<User> findAll(){
        Connection conn = JDBCUtils.getConnection();

        List<User> list=new ArrayList<>();
        ResultSet set=null;
        Statement statement=null;
        try{
            statement = conn.createStatement();

            String sql = "select * from users";

            set = statement.executeQuery(sql);

            while (set.next()) {
            /*
            User user = new User();
            user.setId(set.getInt("id"));
            user.setUsername(set.getString("username"));
            user.setPassword(set.getString("password"));
            */

                User user = new User(set.getInt(1),set.getString(2),set.getString(3));

                list.add(user);
            }
            return list;
        }catch(SQLException e){
            e.printStackTrace();
        }finally {
            if(set!=null){
                try {
                    set.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(statement!=null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn!=null){
                JDBCUtils.close();
            }
        }
        return null;
    }
    public User findById(int id){
        Connection conn = JDBCUtils.getConnection();

        ResultSet set=null;
        /*
        Statement statement=null;
        */
        PreparedStatement ps=null;
        try{
            /*
            statement = conn.createStatement();
            String sql = "select * from users where id ="+id;
            set = statement.executeQuery(sql);
            */
            String sql="select * from users where id = ?";
            ps=conn.prepareStatement(sql);
            ps.setInt(1,id);
            set=ps.executeQuery();
            while (set.next()) {
            /*
            User user = new User();
            user.setId(set.getInt("id"));
            user.setUsername(set.getString("username"));
            user.setPassword(set.getString("password"));
            */
                User user = new User(set.getInt(1),set.getString(2),set.getString(3));
                return user;
            }
        }catch(SQLException e){
            e.printStackTrace();
        }finally {
            if(set!=null){
                try {
                    set.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(ps!=null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn!=null){
                JDBCUtils.close();
            }
        }
        return null;
    }
    public boolean insert(User user) throws SQLException {
        Connection conn = JDBCUtils.getConnection();
        PreparedStatement ps=null;
        try{
            String sql="insert into users values(?,?,?)";
            ps=conn.prepareStatement(sql);
            ps.setInt(1,user.getId());
            ps.setString(2,user.getUsername());
            ps.setString(3,user.getPassword());
            ps.executeUpdate();
        }catch(SQLException e){
            e.printStackTrace();
        }finally {
            if(ps!=null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn!=null){
                JDBCUtils.close();
            }
        }
        return false;
    }
    public boolean delete(int id){
        Connection conn = JDBCUtils.getConnection();
        PreparedStatement ps=null;
        try{
            String sql="delete from users where id = ?";
            ps=conn.prepareStatement(sql);
            ps.setInt(1,id);
            ps.executeUpdate();
        }catch(SQLException e){
            e.printStackTrace();
        }finally {
            if(ps!=null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn!=null){
                JDBCUtils.close();
            }
        }
        return false;
    }
    public boolean update(User user){
        Connection conn = JDBCUtils.getConnection();
        PreparedStatement ps=null;
        try{
            String sql="update users set username=?,password=? where id=?";
            ps=conn.prepareStatement(sql);

            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());
            ps.setInt(3,user.getId());
            ps.executeUpdate();
        }catch(SQLException e){
            e.printStackTrace();
        }finally {
            if(ps!=null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn!=null){
                JDBCUtils.close();
            }
        }
        return false;
    }
}
